package com.xgg.taiwuinfo.filter;

import android.content.Context;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class Filter {
    public String mName;
    public String mTempName;

    public ArrayList<FilterComponent> mList;

    static public Filter Init(JSONObject obj , Context ctx)
    {
        try{
            Filter ft = new Filter();
            ft.mName = obj.getString("n");
            ft.mTempName = ft.mName;
            JSONArray arr = obj.getJSONArray("fc");
            ft.mList = new ArrayList<>();
            for (int i = 0; i < arr.length(); i++)
            {
                FilterComponent fc = FilterComponent.LoadJson((JSONObject)arr.get(i) , 0 , ctx);
                if (fc != null)
                {
                    ft.mList.add(fc);
                }
            }
            return ft;
        }
        catch (Exception e){}
        return null;
    }

    static public Filter Init(String name)
    {
        try{
            Filter ft = new Filter();
            ft.mName = name;
            ft.mTempName = ft.mName;
            ft.mList = new ArrayList<>();
            return ft;
        }
        catch (Exception e){}
        return null;
    }

    public void save()
    {
        mName = mTempName;
        for (int i = 0; i < mList.size(); i++)
        {
            mList.get(i).save();
        }
    }

    public JSONObject getJsonObj()
    {
        JSONObject obj = new JSONObject();
        try{
            obj.put("n" , mName);
            JSONArray arr = new JSONArray();
            for (int i = 0; i < mList.size(); i++)
            {
                arr.put(mList.get(i).getJsonObj());
            }
            obj.put("fc" , arr);
        }
        catch (Exception e)
        {

        }
        return obj;
    }
}
